package cn.bestick.examsys.dao;

import cn.bestick.examsys.model.Student;
import cn.bestick.examsys.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * Created by IntelliJ IDEA.
 * User: Bestick
 * Date: 2016/4/5
 * Time: 18:00
 * Url: http://www.bestick.cn
 * Copyright © 2015-2016 All rights reserved
 * -----------------------------------------------------------
 * 会当凌绝顶，一览众山小。
 */
public class StudentDao {

    public Student login(Student student) throws Exception {
        //获取Session
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        //开启事务
        session.beginTransaction();

        Query query = session.createQuery("from Student as s where s.id = :id and s.password = :password")
                .setString("id", student.getId())
                .setString("password", student.getPassword());
        Student resultStu = (Student) query.uniqueResult();
        //提交事务
        session.getTransaction().commit();
        return resultStu;
    }
}
